import request from '../utils/request'

export interface Exercise {
  exerciseId: string
  exerciseContent: string
  exerciseType: string
  exerciseDifficulty: string
  exerciseAnswer: string
  exerciseSymbol?: string
  paperTitle?: string
}

export interface Paper {
  paperId: number
  paperTitle: string
  paperContent?: string
}

export interface ExerciseListParams {
  page: number
  size: number
  keyword?: string
  type?: string
  paperId?: number
  courseId?: string
  difficulty?: number | null
}

export interface ExerciseListResponse {
  list: Exercise[]
  total: number
}

export interface ExerciseAnalysis {
  progress: number
  weaknesses: string[]
  recommendations: Exercise[]
}

export interface ProgressAnalysis {
  percentage: number
  totalExercises: number
  completedExercises: number
}

export interface WeaknessAnalysis {
  weakAreas: string[]
  improvementSuggestions: string[]
  performanceStats: {
    [key: string]: number
  }
}

export interface Course {
  courseId: number
  courseName: string
}

// 难度等级枚举
export enum DifficultyLevel {
  EASY = 1,
  MEDIUM = 2,
  HARD = 3,
  VERY_HARD = 4,
  EXTREME = 5
}

// 难度等级标签
export const difficultyLabels: Record<DifficultyLevel, string> = {
  [DifficultyLevel.EASY]: '入门',
  [DifficultyLevel.MEDIUM]: '基础',
  [DifficultyLevel.HARD]: '进阶',
  [DifficultyLevel.VERY_HARD]: '挑战',
  [DifficultyLevel.EXTREME]: '极限'
}

// 难度等级中文数字映射
export const difficultyChineseNumerals: Record<DifficultyLevel, string> = {
  [DifficultyLevel.EASY]: '一',
  [DifficultyLevel.MEDIUM]: '二',
  [DifficultyLevel.HARD]: '三',
  [DifficultyLevel.VERY_HARD]: '四',
  [DifficultyLevel.EXTREME]: '五'
}

// 根据难度等级获取中文数字
export function getDifficultyLabel(level: DifficultyLevel): string {
  return difficultyChineseNumerals[level] || ''
}

// 根据中文数字获取难度等级
export function getDifficultyByLabel(label: string): DifficultyLevel {
  const entries = Object.entries(difficultyChineseNumerals)
  const found = entries.find(([_, value]) => value === label)
  if (found) {
    return Number(found[0]) as DifficultyLevel
  }
  throw new Error('无效的难度标签: ' + label)
}

export interface ExerciseForm {
  exerciseType: string
  exerciseDifficulty: string
  courseName: string
  paperTitle: string
}

export interface BaseResponse<T> {
  code: number
  message: string
  data: T
}

// API 函数
export const getCourseList = () => {
  return request.get<BaseResponse<Course[]>>('/course/list')
}

export const getPaperList = () => {
  return request.post<BaseResponse<Paper[]>>('/exercise/get/all')
}

export const createExercise = (data: ExerciseForm) => {
  return request.post<BaseResponse<void>>('/user/exercise', data)
}

export const getExerciseList = (params: ExerciseListParams) => {
  return request.get<BaseResponse<ExerciseListResponse>>('/exercise/list', { params })
}

export const getExerciseDetail = (id: string) => {
  return request.get<BaseResponse<Exercise>>(`/exercise/getExercise/${id}`)
}

export const getExerciseAnalysis = (id: string) => {
  return request.get<BaseResponse<ProgressAnalysis>>(`/exercise/analyze/progress/${id}`)
}

export const getExerciseWeakness = () => {
  return request.get<BaseResponse<WeaknessAnalysis>>('/exercise/analyze/weakness')
}

export const getRecommendExercises = () => {
  return request.get<BaseResponse<Exercise[]>>('/exercise/recommend')
}

export const getErrorRates = (exerciseIds: string[]) => {
  return request.get<BaseResponse<Record<string, number>>>('/exercise/error-rates', {
    params: { exerciseIds }
  })
}